package com.smart.business.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.smart.business.utils.ChartUtil;
import com.smart.entity.business.JwznGasDataEntity;
import com.smart.mybatis.service.impl.BaseServiceImpl;
import com.smart.business.dao.JwznGasDataDao;
import com.smart.service.business.JwznGasDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * gas ServiceImpl
 *
 * @author wf
 * @since 2025-05-13 17:30:15
 */
@Service("jwznGasDataService")
@Transactional(rollbackFor = Exception.class)
public class JwznGasDataServiceImpl extends BaseServiceImpl<JwznGasDataDao, JwznGasDataEntity> implements JwznGasDataService {



    @Autowired
    private JwznGasDataDao jwznGasDataMapper;


    @Override
    public List<Map<String, Object>> getYesterLineChartData(String name) {
        return ChartUtil.getYesterLineChartData(
                (Function<QueryWrapper<JwznGasDataEntity>, List<JwznGasDataEntity>>) wrapper ->
                        jwznGasDataMapper.selectList(wrapper),
                name
        );//F<T輸入類型,R返回類型>為函數式接口
        //w -> lambda表達式,實現F接口的apply()方法
    }
    @Override
    public List<Map<String, Object>> getDailyLineChartData(String name) {
        return ChartUtil.getDailyLineChartData(
                (Function<QueryWrapper<JwznGasDataEntity>, List<JwznGasDataEntity>>) wrapper ->
                        jwznGasDataMapper.selectList(wrapper),
                name
        );//F<T輸入類型,R返回類型>為函數式接口
        //w -> lambda表達式,實現F接口的apply()方法
    }


    @Override
    public List<Map<String, Object>> getWeeklyLineChartData(String name) {
        return ChartUtil.getWeeklyLineChartData(
                (Function<QueryWrapper<JwznGasDataEntity>, List<JwznGasDataEntity>>) wrapper ->
                        jwznGasDataMapper.selectList(wrapper),
                name
        );
    }

    @Override
    public List<Map<String, Object>> getMonthlyLineChartData(String name) {
        return ChartUtil.getMonthlyLineChartData(
                (Function<QueryWrapper<JwznGasDataEntity>, List<JwznGasDataEntity>>) wrapper ->
                        jwznGasDataMapper.selectList(wrapper),
                name
        );
    }

    @Override
    public List<Map<String, Object>> getYesterPieChartDataByLevel(String name) {
        List<String> rawData = jwznGasDataMapper.getPieChartDataByLevel(name);
        return ChartUtil.pieChartData(
                rawData,
                name,
                lowName -> jwznGasDataMapper.getYesterConsumptionStats(lowName),
                jwznGasDataMapper::getYesterConsumptionStats
        );
    }

    @Override
    public List<Map<String, Object>> getDailyPieChartDataByLevel(String name) {
        List<String> rawData = jwznGasDataMapper.getPieChartDataByLevel(name);
        return ChartUtil.pieChartData(
                rawData,
                name,
                lowName -> jwznGasDataMapper.getDailyConsumptionStats(lowName),
                jwznGasDataMapper::getDailyConsumptionStats
        );
    }

    @Override
    public List<Map<String, Object>> getWeeklyPieChartDataByLevel(String name) {
        List<String> rawData = jwznGasDataMapper.getPieChartDataByLevel(name);
        return ChartUtil.pieChartData(
                rawData,
                name,
                lowName -> jwznGasDataMapper.getWeeklyConsumptionStats(lowName),
                jwznGasDataMapper::getWeeklyConsumptionStats
        );
    }

    @Override
    public List<Map<String, Object>> getMonthlyPieChartDataByLevel(String name) {
        List<String> rawData = jwznGasDataMapper.getPieChartDataByLevel(name);
        return ChartUtil.pieChartData(
                rawData,
                name,
                lowName -> jwznGasDataMapper.getMonthlyConsumptionStats(lowName),
                jwznGasDataMapper::getMonthlyConsumptionStats
        );
    }


    public List<Map<String, Integer>> getBarChartDataBy2Time(LocalDateTime startTime, LocalDateTime endTime) {
        return jwznGasDataMapper.getBarChartDataBy2Time(startTime,endTime);
    }

    @Override
    public Double getDailyConsumptionStats(String name) {
        return jwznGasDataMapper.getDailyConsumptionStats(name);
    }
    @Override
    public Double getYesterConsumptionStats(String name) {
        return jwznGasDataMapper.getYesterConsumptionStats(name);
    }

    @Override
    public Double getWeeklyConsumptionStats(String name) {
        return jwznGasDataMapper.getWeeklyConsumptionStats(name);
    }

    @Override
    public Double getMonthlyConsumptionStats(String name) {
        return jwznGasDataMapper.getMonthlyConsumptionStats(name);
    }
}